using Microsoft.EntityFrameworkCore;
using PandaWebAPI.Models.ef;

namespace PandaWebAPI.Services
{
    public class LikeService : BaseService<Like>, ILikeService
    {
        public LikeService(PandaSystemContext context) : base(context)
        {
        }

        public async Task<List<Like>> GetLikesByPostIdAsync(Guid postId)
        {
            return await _dbSet
                .Include(l => l.User)
                .Where(l => l.PostId == postId)
                .OrderByDescending(l => l.CreatedAt)
                .ToListAsync();
        }

        public async Task<List<Like>> GetLikesByCommentIdAsync(Guid commentId)
        {
            return await _dbSet
                .Include(l => l.User)
                .Where(l => l.CommentId == commentId)
                .OrderByDescending(l => l.CreatedAt)
                .ToListAsync();
        }

        public async Task<List<Like>> GetLikesByUserIdAsync(Guid userId)
        {
            return await _dbSet
                .Include(l => l.Post)
                .Include(l => l.Comment)
                .Where(l => l.UserId == userId)
                .OrderByDescending(l => l.CreatedAt)
                .ToListAsync();
        }

        public async Task<bool> ToggleLikeAsync(Guid userId, Guid? postId, Guid? commentId)
        {
            var existingLike = await _dbSet
                .FirstOrDefaultAsync(l => l.UserId == userId && 
                    ((postId.HasValue && l.PostId == postId) || 
                     (commentId.HasValue && l.CommentId == commentId)));

            if (existingLike != null)
            {
                // 如果已经点赞，则取消点赞
                _dbSet.Remove(existingLike);
                await _context.SaveChangesAsync();
                return false;
            }
            else
            {
                // 如果未点赞，则添加点赞
                var newLike = new Like
                {
                    LikeId = Guid.NewGuid(),
                    UserId = userId,
                    PostId = postId,
                    CommentId = commentId,
                    CreatedAt = DateTime.UtcNow
                };

                await _dbSet.AddAsync(newLike);
                await _context.SaveChangesAsync();
                return true;
            }
        }

        public async Task<bool> HasUserLikedAsync(Guid userId, Guid? postId, Guid? commentId)
        {
            return await _dbSet
                .AnyAsync(l => l.UserId == userId && 
                    ((postId.HasValue && l.PostId == postId) || 
                     (commentId.HasValue && l.CommentId == commentId)));
        }

        public override async Task<Like> CreateAsync(Like like)
        {
            // 检查是否已经存在相同的点赞
            var existingLike = await _dbSet
                .FirstOrDefaultAsync(l => l.UserId == like.UserId && 
                    ((like.PostId.HasValue && l.PostId == like.PostId) || 
                     (like.CommentId.HasValue && l.CommentId == like.CommentId)));

            if (existingLike != null)
            {
                throw new InvalidOperationException("用户已经点赞过该内容");
            }

            like.LikeId = Guid.NewGuid();
            like.CreatedAt = DateTime.UtcNow;
            
            await _dbSet.AddAsync(like);
            await _context.SaveChangesAsync();
            
            return like;
        }

        public override async Task<bool> DeleteAsync(Guid id)
        {
            var like = await _dbSet.FindAsync(id);
            if (like == null)
                return false;

            _dbSet.Remove(like);
            await _context.SaveChangesAsync();
            
            return true;
        }
    }
} 